package cn.study.aviplayer;

import android.app.Activity;
import android.app.AlertDialog;

import java.io.IOException;

public abstract class AbstractPlayerActivity extends Activity {

    public static final String EXTRA_FILE_NAME = "cn.study.aviplayer.EXTRA_FILE_NAME";

    protected long avi = 0;

    @Override
    protected void onStart() {
        super.onStart();

        try {

            avi = open(getFileName());
        } catch (IOException e) {

            new AlertDialog.Builder(this)
                    .setTitle("错误提示")
                    .setMessage(e.getMessage())
                    .show();
        }
    }


    @Override
    protected void onStop() {
        super.onStop();

        if (0 != avi) {

            close(avi);
            avi = 0;
        }
    }

    protected String getFileName() {

        return getIntent().getExtras().getString(EXTRA_FILE_NAME);
    }


    /**
     * 打开指定的avi文件
     *
     * @return
     */
    protected native static long open(String fileName) throws IOException;

    /**
     * 获取视频宽度
     *
     * @param avi
     * @return
     */
    protected native static int getWidth(long avi);

    /**
     * 获取视频高度
     *
     * @param avi
     * @return
     */
    protected native static int getHeight(long avi);


    /**
     * 获取帧速
     *
     * @param avi
     * @return
     */
    protected native static double getFrameRate(long avi);

    /**
     * 基于给定的文件描述符关闭指定的avi文件
     *
     * @param avi
     */
    protected native static void close(long avi);

}
